package com.atgugui.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atgugui.eduservice.Listener.SubjectExcelListener;
import com.atgugui.eduservice.entity.EduSubject;
import com.atgugui.eduservice.entity.ExcelSubjectData;
import com.atgugui.eduservice.entity.subject.OneSubject;
import com.atgugui.eduservice.entity.subject.SecondSubject;
import com.atgugui.eduservice.mapper.EduSubjectMapper;
import com.atgugui.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-26
 */
@SuppressWarnings("all")
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void addSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelSubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询出所有的一级分类及该一级分类下的所有二级分类，封装到OneSubject中
     * 最终将所有的OneSubject放置到List中返回即可
     * 思路：
     *      1. 查询出所有的一级分类
     *      2. 遍历每一个一级分类，假设当前一级分类的id = xxx
     *         遍历所有的Subject，如果该Subject的parentId = xxx，说明当前的subject就是该
     *         一级分类下的二级分类
     * @return
     */
    @Override
    public List<OneSubject> getAllOneSubject() {
        // 查询所有一级分类
        QueryWrapper<EduSubject> oneQuery = new QueryWrapper<>();
        oneQuery.eq("parent_id","0");
        List<EduSubject> OneEduSubjectList = baseMapper.selectList(oneQuery);

        // 查询所有二级分类
        QueryWrapper<EduSubject> secondQuery = new QueryWrapper<>();
        secondQuery.ne("parent_id","0");
        List<EduSubject> SecondEduSubjectList = baseMapper.selectList(secondQuery);

        List<OneSubject> oneSubjectList = new ArrayList<>();
        for (EduSubject eduSubject : OneEduSubjectList) {
            OneSubject oneSubject = new OneSubject();
            oneSubject.setTitle(eduSubject.getTitle());
            oneSubject.setId(eduSubject.getId());
            oneSubjectList.add(oneSubject);
        }

        for (OneSubject oneSubject : oneSubjectList) {
            for (EduSubject eduSubject : SecondEduSubjectList) {
                // 注意：这里不能直接使用 == 判断相等，因为id和parentId都是String类型
                // 不是常量类型
                if (oneSubject.getId().equals(eduSubject.getParentId())) {
                    SecondSubject secondSubject = new SecondSubject();
                    secondSubject.setTitle(eduSubject.getTitle());
                    secondSubject.setId(eduSubject.getId());
                    if (oneSubject.getChildren() == null) {
                        List<SecondSubject> secondSubjectList = new ArrayList<>();
                        secondSubjectList.add(secondSubject);
                        oneSubject.setChildren(secondSubjectList);
                    } else {
                        oneSubject.getChildren().add(secondSubject);
                    }
                }
            }
        }
        return oneSubjectList;
    }
}
